package com.zzti.controller;

import com.zzti.pojo.SysResult;
import com.zzti.pojo.Visiter;
import com.zzti.serviceImpl.VisiterServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class VisiterController {

    @Autowired
    private VisiterServiceImpl visiterServiceImpl;

    /**
     * 访客查看自身或他人信息,根据参数id来查找个人信息,返回一个Visiter对象
     *
     * @param id
     * @return
     */
    @RequestMapping("visiter/findOne")
    @ResponseBody
    public Visiter findOne(Integer id) {
        return visiterServiceImpl.findOne(id);
    }

    /**
     * 管理员或服务员查看所有的访客信息，无参数，返回一个包含所有Visiter对象的列表
     *
     * @return
     */
    @RequestMapping("visiter/findAll")
    @ResponseBody
    public List<Visiter> findAll() {
//        List<Visiter> ls = visiterServiceImpl.findAll();
//        for (Visiter vs:ls
//             ) {
//            System.out.println(vs.toString());
//        }
//        return null;
        return visiterServiceImpl.findAll();
    }

    /**
     * 管理员或服务员删除访客信息，参数id数组，返回对象SysResult.status=200则表示成功
     *
     * @param ids
     * @return
     */
    @RequestMapping("visiter/delete")
    @ResponseBody
    public SysResult deleteById(Integer[] ids) {
//        Integer[] in = new Integer[2];
//        in[0]=1;
//        in[1]=2;
//        System.out.println(visiterServiceImpl.deleteByIds(in));
//        return null;
        if (visiterServiceImpl.deleteByIds(ids) > 0)
            return SysResult.success();
        else
            return SysResult.fail();
    }

    /**
     * 用户注册时调用,参数Visiter对象，返回值大于0则成功
     *
     * @param vs
     * @return
     */
    @RequestMapping("visiter/add")
    @ResponseBody
    public SysResult add(Visiter vs) {
//        Visiter vs1=new Visiter(4, "qwe", "www", "zxc", "qwe", "sdfsf", 12, 1, 0, null, null);
//        System.out.println(visiterServiceImpl.add(vs1));
//        return null;
        if (visiterServiceImpl.add(vs) > 0)
            return SysResult.success();
        else
            return SysResult.fail();
    }

    /**
     * 用户修改信息时调用,参数Visiter对象，返回值大于0则成功
     *
     * @param vs
     * @return
     */
    @RequestMapping("visiter/update")
    @ResponseBody
    public SysResult update(Visiter vs) {
//        Visiter vs1=new Visiter(4, "qweqwe", "www", "zxc", "qwe", "sdfsf", 12, 1, 0, null, null);
//        System.out.println(visiterServiceImpl.update(vs1));
//        return null;
        if (visiterServiceImpl.update(vs) > 0)
            return SysResult.success();
        else
            return SysResult.fail();
    }

    @RequestMapping("/index1")
    public String index() {
        return "redirect:website.html";
    }

    @RequestMapping("visiter/login")
    public String login(String account, String password, Model model) {
        System.out.println(account);
        System.out.println(password);
        Visiter vis = visiterServiceImpl.login(account, password);
        if (vis != null) {
            model.addAttribute("user", vis);
            System.out.println(vis.toString());
            return "redirect:/user.html";
        } else return "redirect:/login.html";
    }
}
